package com.mms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.mms.dao.ApplyMeetingDao;
import com.mms.dao.MeetingManagementDao;
import com.mms.entity.Configure;
import com.mms.entity.ConfigureId;
import com.mms.entity.Device;
import com.mms.entity.DeviceCount;
import com.mms.entity.Meeting;
import com.mms.entity.MeetingRoom;
import com.mms.entity.Staff;
import com.mms.util.DateFormat;
import com.mms.util.MyMeetingInfo;
import com.opensymphony.xwork2.Action;

public class MeetingAction implements Action {
    
    private Integer meetingRoomID;
    private String content;
    private String startTime;
    private String endTime;
    private String topic;
    private Integer[] devices;
    private Integer capacity;
    
    
    public String applyMeetingByTimeAndNumber() {
        return SUCCESS;
    }
    public String getMeetingRoomByTimeAndNumber() throws IOException {
        ApplyMeetingDao applyMeetingDao=new ApplyMeetingDao();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date startDate=new Date(Long.parseLong(startTime));
        Date endDate=new Date(Long.parseLong(endTime));
        List<MeetingRoom>  meetingRooms=applyMeetingDao.getMeetingRoomsByTimeAndCapacity(sdf.format(startDate), sdf.format(endDate), capacity);
        JSONArray jsonArray = JSONArray.fromObject(meetingRooms); 
        System.out.println(jsonArray.toString());
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(jsonArray.toString());
        out.flush();
        out.close();
        return SUCCESS;
    }
    
    public String getDeviceByTimeAndNumber() throws IOException {
        ApplyMeetingDao applyMeetingDao=new ApplyMeetingDao();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date startDate=new Date(Long.parseLong(startTime));
        Date endDate=new Date(Long.parseLong(endTime));
        List<Device> deList = applyMeetingDao.getDevicesByTimeAndNumber(sdf.format(startDate), sdf.format(endDate));
        JSONArray jsonArray = JSONArray.fromObject(deList); 
        System.out.println(jsonArray.toString());
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(jsonArray.toString());
        out.flush();
        out.close();
        return SUCCESS;
    }
    
    public String getDevicesCountByTimeAndNumber() throws IOException {
        ApplyMeetingDao applyMeetingDao=new ApplyMeetingDao();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date startDate=new Date(Long.parseLong(startTime));
        Date endDate=new Date(Long.parseLong(endTime));
        List<DeviceCount> dCounts = applyMeetingDao.getDevicesCountByTimeAndNumber(sdf.format(startDate), sdf.format(endDate));
        JSONArray jsonArray = JSONArray.fromObject(dCounts); 
        System.out.println(jsonArray.toString());
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(jsonArray.toString());
        out.flush();
        out.close();
        return SUCCESS;
    }
    
    
       public String isTopicExist() throws Exception {
           List<String> strings=new ArrayList<String>();
           ApplyMeetingDao applyMeetingDao=new ApplyMeetingDao();
           String result;
           System.out.println(URLDecoder.decode(topic, "UTF-8"));
           if (applyMeetingDao.isTopicExist(URLDecoder.decode(topic, "UTF-8"))) result="true";
           else result="false";
           strings.add(result);
           JSONArray jsonArray = JSONArray.fromObject(strings); 
           HttpServletResponse response=ServletActionContext.getResponse();
           response.setContentType("text/html;charset=utf-8");
           PrintWriter out = response.getWriter();
           System.out.println(jsonArray.toString());
           out.print(jsonArray.toString());
           out.flush();
           out.close();
           return SUCCESS;
    }
       
      public String getMeetingByStartTimeAndAddress() throws IOException {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          Date startDate=new Date(Long.parseLong(startTime));
          ApplyMeetingDao applyMeetingDao=new ApplyMeetingDao();
          List<Meeting> meetings=applyMeetingDao.getMeetingsByStartTimeAndAddress(meetingRoomID, sdf.format(startDate));
          JSONArray jsonArray = JSONArray.fromObject(meetings); 
          HttpServletResponse response=ServletActionContext.getResponse();
          response.setContentType("text/html;charset=utf-8");
          PrintWriter out = response.getWriter();
          System.out.println(jsonArray.toString());
          out.print(jsonArray.toString());
          out.flush();
          out.close();
          return SUCCESS;
    }
      
      public String getMeetingRooms() throws UnsupportedEncodingException {
          HttpServletRequest request=ServletActionContext.getRequest();
          request.setCharacterEncoding("UTF-8");
          ApplyMeetingDao applyMeetingDao=new ApplyMeetingDao();
          List<MeetingRoom> meetingRooms=applyMeetingDao.getMeetingRooms();
          request.setAttribute("meetingRoomList", meetingRooms);
          return SUCCESS;
    }
      
      public String isTimeLegal() throws IOException {
          List<String> strings=new ArrayList<String>();
          ApplyMeetingDao applyMeetingDao=new ApplyMeetingDao();
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
          Date startDate=new Date(Long.parseLong(startTime));
          Date endDate=new Date(Long.parseLong(endTime));
          String result;
          if (applyMeetingDao.isTimeLegal(sdf.format(startDate), sdf.format(endDate), meetingRoomID))  result="true";
          else result="false";
          strings.add(result);
          JSONArray jsonArray = JSONArray.fromObject(strings);
          HttpServletResponse response=ServletActionContext.getResponse();
          response.setContentType("text/html;charset=utf-8");
          PrintWriter out = response.getWriter();
          System.out.println(jsonArray.toString());
          out.print(jsonArray.toString());
          out.flush();
          out.close();
          return SUCCESS;
    }
    public String applyMeeting() throws Exception {
        HttpServletRequest request=ServletActionContext.getRequest();
        request.setCharacterEncoding("UTF-8");
        ApplyMeetingDao applyMeetingDao=new ApplyMeetingDao();
        Meeting meeting=new Meeting();
        MeetingRoom meetingRoom=new MeetingRoom(meetingRoomID);
        meeting.setMeetingRoom(meetingRoom);
        Staff staff=new Staff(Integer.parseInt(request.getSession().getAttribute("userID").toString()));
        meeting.setStaff(staff);
        meeting.setContent(content);
        meeting.setStartTime(Timestamp.valueOf(DateFormat.dateTimeFormat(startTime)));
        meeting.setEndTime(Timestamp.valueOf(DateFormat.dateTimeFormat(endTime)));
        meeting.setTopic(topic);
        meeting.setState(0);
        if(!applyMeetingDao.isTimeLegal(meeting.getStartTime().toString(), meeting.getEndTime().toString(), meetingRoomID)){
            request=MyMeetingInfo.getMyMeetingInfo(request);
            request.setAttribute("isLegalFlag", 1);
            return ERROR;
        }
        Configure configure=new Configure();
        ConfigureId configureId=new ConfigureId();
        configureId.setTopic(topic);
        for(int i=0;i<devices.length;i++){
            configureId.setDId(devices[i]);
            if (!applyMeetingDao.isConfigureExist(configureId)) {
                configure.setId(configureId);
                applyMeetingDao.bind(configure);
            }
        }//for
        
        if (applyMeetingDao.addMeeting(meeting)) {
            request.setAttribute("isApplySuccessful", true);
        }
        else {
            request.setAttribute("isApplySuccessful", false);
        }
        request=MyMeetingInfo.getMyMeetingInfo(request);
        return SUCCESS;
    }
    @Override
    public String execute() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }
    
    
    public Integer getMeetingRoomID() {
        return meetingRoomID;
    }
    public void setMeetingRoomID(Integer meetingRoomID) {
        this.meetingRoomID = meetingRoomID;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getStartTime() {
        return startTime;
    }
    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }
    public String getEndTime() {
        return endTime;
    }
    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }
    public String getTopic() {
        return topic;
    }
    public void setTopic(String topic) {
        this.topic = topic;
    }
    
    public Integer[] getDevices() {
        return devices;
    }
    public void setDevices(Integer[] devices) {
        this.devices = devices;
    }
    public Integer getCapacity() {
        return capacity;
    }
    public void setCapacity(Integer capacity) {
        this.capacity = capacity;
    }
   

}
